What is @types/unist?
The @types/unist package provides TypeScript definitions for Unist, a specification for syntax trees. It is used to add static typing to Unist-based projects, enabling developers to work with Unist syntax trees in a type-safe manner. This package is essential for TypeScript developers working with Unist or any Unist-compatible syntax tree, such as Markdown Abstract Syntax Trees (ASTs), providing auto-completion and type checking.
What are @types/unist's main functionalities?
Node Type Definition
Defines the basic structure of a Unist node, including types like 'text', 'element', etc. This is fundamental for creating and manipulating syntax trees.
{
"type": "text",
"value": "Hello, world!"
}
Parent Type Definition
Defines a parent node which can contain other nodes. This is crucial for representing hierarchical structures within syntax trees.
{
"type": "paragraph",
"children": [
{
"type": "text",
"value": "Hello, world!"
}
]
}
Literal Type Definition
Defines a literal node with a value. This is used for nodes that contain actual content, such as text.
{
"type": "text",
"value": "Example text"
}
Other packages similar to @types/unist
@types/mdast
Provides TypeScript definitions for MDAST, a Markdown Abstract Syntax Tree format. It is similar to @types/unist in that it offers type definitions for a specific syntax tree format, but it is specifically tailored to Markdown, whereas @types/unist is more general.
@types/hast
Offers TypeScript definitions for HAST, an HTML Abstract Syntax Tree format. Like @types/unist, it provides type definitions for working with syntax trees, but focuses on HTML content.
remark
A Markdown processor powered by plugins part of the unified ecosystem. While not a type package itself, it uses Unist under the hood for its syntax tree. It demonstrates a practical application of Unist, showing how @types/unist can be applied in real-world projects.
Installation
npm install --save @types/unist
Summary
This package contains type definitions for unist (https://github.com/syntax-tree/unist).
Details
Files were exported from https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/unist/v2.
export interface Node<TData extends object = Data> {
type: string;
data?: TData | undefined;
position?: Position | undefined;
}
export interface Data {
[key: string]: unknown;
}
export interface Position {
start: Point;
end: Point;
indent?: number[] | undefined;
}
export interface Point {
line: number;
column: number;
offset?: number | undefined;
}
export type NodeData<TNode extends Node<object>> = TNode extends Node<infer TData> ? TData : never;
export interface Parent<ChildNode extends Node<object> = Node, TData extends object = NodeData<ChildNode>>
extends Node<TData>
{
children: ChildNode[];
}
export interface Literal<Value = unknown, TData extends object = Data> extends Node<TData> {
value: Value;
}
Additional Details
- Last updated: Thu, 15 Aug 2024 02:18:53 GMT
- Dependencies: none
Credits
These definitions were written by bizen241, Jun Lu, Hernan Rajchert, Titus Wormer, Junyoung Choi, Ben Moon, and JounQin.